<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/3/1
 * Time: 10:37
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class About extends Base
{
    /**
     * 关于我们
     * @return mixed|\think\response\Json
     */
    public function aboutIndex(){
        if(Request::instance()->isPost()){
            $about_us = input('about_us');//内容
            $res = Db::table('up_about')->where('id', 1)->setField('content', $about_us);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $about_us = Db::table('up_about')->where('id', 1)->value('content');
        $this->assign('about_us', $about_us);
        return $this->fetch('about/aboutindex');
    }
    
    /**
     * 客服
     * @return mixed|\think\response\Json
     */
    public function service(){
        if(Request::instance()->isPost()){
            $phone = input('phone');
            $res = Db::table('up_about')->where('id', 3)->setField('content', $phone);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $phone = Db::table('up_about')->where('id', 3)->value('content');
        $this->assign('phone', $phone);
        return $this->fetch('about/service');
    }
    
    /**
     * 用户协议
     * @return mixed|\think\response\Json
     */
    public function agreement(){
        if(Request::instance()->isPost()){
            $agreement = input('agreement');//内容
            $res = Db::table('up_about')->where('id', 2)->setField('content', $agreement);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $agreement = Db::table('up_about')->where('id', 2)->value('content');
        $this->assign('agreement', $agreement);
        return $this->fetch('about/agreement');
    }
    
    /**
     * 换货说明
     * @return mixed|\think\response\Json
     */
    public function changeExplain(){
        if(Request::instance()->isPost()){
            $explain = input('explain');//内容
            $res = Db::table('up_about')->where('id', 5)->setField('content', $explain);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $explain = Db::table('up_about')->where('id', 5)->value('content');
        $this->assign('explain', $explain);
        return $this->fetch('about/explain');
    }
    
    /**
     * 地区查询
     * @return mixed|\think\response\Json
     */
    public function searchArea(){
        if(request()->isAjax ()){
            $district = input('district');
            $level = input('level');
            $where = [];
            //地区名称
            if(isset($district) && $district != ''){
                $where['district'] = ['like', '%' . $district . '%'];
            }
            //等级
            if(isset($level) && $level != ''){
                $where['level'] = $level;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('area')->where($where)->count();// 获取总条数
            $lists = Db::name('area')
                ->field('district_id, district, level')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('district_id asc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('about/searcharea');
    }
}